package com.common;

import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class publicMethod {
    @Autowired
    private RestTemplate restTemplate;
    public  String getNowDate() {
    	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SS");//设置日期格式
        return df.format(new Date());// new Date()为获取当前系统时间
    }
    public  String getNowDateShort() {
    	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        return df.format(new Date());// new Date()为获取当前系统时间
    }

    /**
     * 查询全量的城市
     * @param sqlCookie
     * @return
     */
    public List<String> getAllCity(String sqlCookie) {
        com.alibaba.fastjson.JSONObject param = new com.alibaba.fastjson.JSONObject();
        param.put("@column", "CityCaption");
        param.put("State", "1");
        param.put("@order", "CityIndex");
        com.alibaba.fastjson.JSONObject param1 = new com.alibaba.fastjson.JSONObject();
        param1.put("Archive_CityInfo", param);
        param1.put("page", 0);
        param1.put("count", 1000);
        com.alibaba.fastjson.JSONObject param2 = new com.alibaba.fastjson.JSONObject();
        param2.put("[]", param1);

        String sr = HttpRequest.sendPostAPI("get", param2.toString(), sqlCookie);

        net.sf.json.JSONObject sqlData = net.sf.json.JSONObject.fromObject(sr);
        List<String> cityList = new ArrayList<>();
        if (sqlData.getBoolean("ok") == true && sqlData.containsKey("[]")) {
            for (Object s1 : sqlData.getJSONArray("[]")) {
                net.sf.json.JSONObject details = ((JSONObject) s1).getJSONObject("Archive_CityInfo");
                if (details.containsKey("CityCaption")) {
                    cityList.add(details.getString("CityCaption"));
                }
            }
        }
        return cityList;

    }

    public String getSqlCookie() {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity httpEntity = null;
        try {
            //参数
            String str = "{\"phone\":\"18915047090\",\"password\":\"111111\"}";
            httpEntity = new HttpEntity(str, headers);
        } catch (Exception e) {
            log.error("111111111", e);
        }
        ResponseEntity<String> responseEntity = null;
        try {
            String url = "http://cricinapi-dev.cricwifi.com/login";
            responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
        } catch (Exception e) {
            log.error("2222", e);

        }
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            log.error(" 333333333", responseEntity);
        }
        HttpHeaders httpHeaders = responseEntity.getHeaders();
        List<String> cookieList = httpHeaders.get("Set-Cookie");
        return cookieList.get(0);
    }
}
